﻿// Write a program that finds the most frequent number in an array. 
// Example: {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} -> 4 (5 times)

using System;
using System.Collections.Generic;

class FindMostFrequentNumber
{
    static void Main()
    {
        // read array size
        uint arraySize;
        do
        {
            Console.Write("Enter array size: ");
        } while (!uint.TryParse(Console.ReadLine(), out arraySize));

        // read array elements
        int[] intArray = new int[arraySize];
        for (int i = 0; i < intArray.Length; i++)
        {
            do
            {
                Console.Write("Enter array elements {0}: ", i + 1);
            } while (!int.TryParse(Console.ReadLine(), out intArray[i]));
        }

        List<int> elementsList = new List<int>();
        elementsList.Add(intArray[0]);
        int elementCount = 1;
        int elementCountTemp = 1;

        for (int i = 0; i < intArray.Length; i++)
        {
            if (intArray[i] != int.MinValue)
            {
                for (int j = i; j < intArray.Length; j++)
                {
                    if (intArray[j] != int.MinValue && intArray[i] == intArray[j] && i != j)
                    {
                        elementCountTemp++;
                        intArray[j] = int.MinValue;
                    }
                }

                if (elementCountTemp > elementCount)
                {
                    elementCount = elementCountTemp;
                    elementsList.Clear();
                    elementsList.Add(intArray[i]);
                }
                else if (elementCountTemp == elementCount)
                {
                    elementsList.Add(intArray[i]);
                }
                elementCountTemp = 1;
            }
        }

        if (elementsList.Count == 1)
        {
            Console.WriteLine("Most frequent number in Array is: {0} ({1} times)", elementsList[0], elementCount);
        }
        else
        {
            Console.WriteLine("Most frequent numbers in Array are: ");
            for (int i = 0; i < elementsList.Count; i++)
            {
                Console.Write("{0} ({1} times)", elementsList[i], elementCount);
                if (i != elementsList.Count - 1)
                {
                    Console.Write(",");
                }
                Console.WriteLine();
            }
        }
    }
}